import * as chai from 'chai'
import * as command from '../../src/exec/command'
import { ChildProcessWithoutNullStreams, SpawnOptionsWithoutStdio } from 'child_process'

describe('command', function () {
    describe('with options', () => {
        let commandInvoker: command.CommandInvoker

        it('cwd options should be passed in', async () => {
          commandInvoker = new command.CommandInvoker("thecommand", ['args'], {cwd: "/the/working/dir"})
          let spawCommand : any
          let spawArgs : any
          let spawOptions : any

          const fakeSpawn = (command: string, args?: ReadonlyArray<string>, options?: SpawnOptionsWithoutStdio): ChildProcessWithoutNullStreams => {
            spawCommand = command
            spawArgs = args
            spawOptions = options

            const process = {
                on: (event: string, listener: (...args: any[]) => void): any => {
                    if(event == 'exit') {
                        listener(0)
                    }
                     return null
                }
            } as ChildProcessWithoutNullStreams
            return process
          }
          commandInvoker.setSpawn(fakeSpawn)
          await commandInvoker.callCommand()
          chai.expect(spawOptions.cwd).to.equal("/the/working/dir")
        })
    })
})
